package cn.wh.xwm.controller.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Transaction;

import net.sf.json.JSONObject;

import cn.wh.xwm.controller.action.vo.InventoryView;
import cn.wh.xwm.hibernate.dao.Inventoryinfo;
import cn.wh.xwm.hibernate.dao.InventoryinfoDAO;
import cn.wh.xwm.hibernate.dao.InventoryinfoId;

public class InventoryAction extends BaseAction {
	private String customerId;
	private String qcustomerId;
	private String qproductId;
	private String qamount;
	
	
	public void findInventoryByCustomerId() throws IOException{
		InventoryinfoDAO dao = new InventoryinfoDAO();
		List<Inventoryinfo> list = dao.findByCustomerId(customerId);
		List<InventoryView> vlist = new ArrayList<InventoryView>();
		for(Inventoryinfo info:list){
			InventoryView view = new InventoryView();
			view.setAmount(info.getInventoryNumber().toString());
			view.setProductName(info.getProductinfo().getName());
			view.setProductId(info.getId().getProductId());
			view.setCustomerId(info.getId().getCustomerId());
			view.setId(info.getId().getCustomerId()+","+info.getId().getProductId());
			vlist.add(view);
		}
		JSONObject object = new JSONObject();
		object.put("rows", vlist);
		object.put("total", list.size());
		dao.getSession().close();
		this.responseJson(object);
	}

	public void updateByComposeId(){
		InventoryinfoDAO dao = new InventoryinfoDAO();
		InventoryinfoId id = new InventoryinfoId();
		id.setCustomerId(qcustomerId);
		id.setProductId(qproductId);
		Inventoryinfo info = dao.findById(id);
		info.setInventoryNumber(Integer.valueOf(qamount));
		Transaction ts = dao.getSession().getTransaction();
		ts.begin();
		dao.save(info);
		ts.commit();
		dao.getSession().close();
	}
	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public String getQcustomerId() {
		return qcustomerId;
	}

	public void setQcustomerId(String qcustomerId) {
		this.qcustomerId = qcustomerId;
	}

	public String getQproductId() {
		return qproductId;
	}

	public void setQproductId(String qproductId) {
		this.qproductId = qproductId;
	}

	public String getQamount() {
		return qamount;
	}

	public void setQamount(String qamount) {
		this.qamount = qamount;
	}

	
}
